package org.or5e.oss.ws;

import java.util.Properties;
import java.util.Set;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import org.or5e.oss.core.BaseObject;
import org.or5e.oss.ws.datatypes.Warning;
import org.or5e.oss.ws.datatypes.Warning.WarningType;
import org.or5e.oss.ws.datatypes.mail.MailMessage;
import org.or5e.oss.ws.datatypes.mail.SendMailRequest;
import org.or5e.oss.ws.datatypes.mail.SendMailResponse;

public class SendMailService extends BaseObject implements Service {
	private static Properties props = new Properties();
	static {
		props.put("mail.smtp.host", "smtp.jcom.net");
	}
	
	@Override
	public String getName() {
		return "SendMailService";
	}

	public Object processMessage(Object _request) {
		SendMailResponse _sendMailResponse = new SendMailResponse();

		Double debugID = new Double(Math.random());
		debug(debugID+" - Process Message Started ***");
		SendMailRequest _sendMailRequest = (SendMailRequest)_request;
		MailMessage _mailMessage = _sendMailRequest.getMailMessage();
		Warning warning = null;
		try {
			Session session = Session.getDefaultInstance(props, null);
			session.setDebug(Boolean.FALSE);
			debug(debugID+" - Session is Created and Debug Mode is OFF ***");
			Message msg = new MimeMessage(session);
			InternetAddress addressFrom = new InternetAddress(_mailMessage.getFromAddress());
			msg.setFrom(addressFrom);
			debug(debugID+" - Address from :"+addressFrom.getAddress());
			Set<String> _toMailList = _mailMessage.getToAddresses();
			InternetAddress[] addressTo = new InternetAddress[_toMailList.size()];
			debug(debugID+" - To Address Size is "+_toMailList.size());
			int index = 0;
			for (String _toAddress : _toMailList) {
				addressTo[index++] = new InternetAddress(_toAddress);
			}
			debug(debugID+" - Setting Message Properties ***");
			msg.setRecipients(Message.RecipientType.TO, addressTo);
			msg.addHeader("Action", "Mail Send from OpenService System.");
			msg.setSubject(_mailMessage.getSubject());
			msg.setContent(_mailMessage.getMailContent(), "text/plain");
			debug(debugID+" - Done. About to send mail thru Transport");
			Transport.send(msg);
			debug(debugID+" - Send Mail Task is Completed Successfully ***");
			_sendMailResponse.setStatus(Boolean.TRUE);
		}
		catch(AddressException _addressException) {
			error(debugID+""+_addressException.getMessage());
			warning.set_warningLevel(new Integer(3));
			warning.set_warningMessage(_addressException.getMessage());
			warning.set_warningType(WarningType.ERROR);
			_sendMailResponse.setStatus(Boolean.FALSE);
			debug(debugID+" - Address Exception Happened ***");
			debug(debugID+" - Populated the Warning Node for the response ***");
		} 
		catch (MessagingException _messageException) {
			error(debugID+""+_messageException.getMessage());
			warning.set_warningLevel(new Integer(3));
			warning.set_warningMessage(_messageException.getMessage());
			warning.set_warningType(WarningType.ERROR);
			_sendMailResponse.setStatus(Boolean.FALSE);
			debug(debugID+" - Messaging Exception Happened ***");
			debug(debugID+" - Populated the Warning Node for the response ***");
		}
		_sendMailResponse.setWarning(warning);
		_sendMailResponse.setMessageID(debugID.longValue());

		debug(debugID+" - Response Message is Generated Successfully ***");
		debug(debugID+" - Process Message Ended ***");
		return _sendMailResponse;
	}
}